package com.hqyj.utile;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hqyj.pojo.WebSocketInfo;

/**
 * @Description 用于对象和字符串互相转换
 * @Autor 伍军
 * @Date 2022/9/27 9:20
 * @Version 1.0
 **/
public class JsonFormatUtil {

    //把字符串转换成对象
    public static WebSocketInfo getObject(String jsonStr){
        ObjectMapper mapper = new ObjectMapper();
        WebSocketInfo wc =null;
        try {
             wc = mapper.readValue(jsonStr,WebSocketInfo.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return wc;
    }

    //把对象转换成字符串
    public static String getStr(WebSocketInfo wc){
        ObjectMapper mapper = new ObjectMapper();
        String str =null;
        try {
            str = mapper.writeValueAsString(wc);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

}
